<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<th:block th:include="include :: summernote-css" />
<style>
.modal-backdrop{
display: none;}
</style>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
	<form class="form-horizontal m" id="form-question-add">
	<input id="id" name="id" th:value="${question.id}" type="hidden"/>
			<div class="form-group">
			<label class="col-sm-3 control-label">题库/知识点：</label>
			<div class="col-sm-8">
				<input id="qdbid" name="qdbid" th:value="${question.qdbid}" type="hidden"/>
				<input id="treeId" name="kbid" type="hidden" th:value="${question.kbid}"/>
				<input class="form-control" type="text" id="treeName" onclick="selectTree()" readonly="true" th:value="${qdb.name}"/>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">题型：</label>
			<div class="col-sm-8">
				<select id="qtid"  name="qtid" th:value="${question.qtid}"  class="form-control noselect2 selectpicker">
                	<option value=""> -- 请选择题型 -- </option>
                	<option th:each="qtype : ${qtypes}" th:value="${qtype.id}" th:selected="${question.qtid==qtype.id}" th:text="${qtype.name}">
                </select>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">难易度1非常容易-5非常难：</label>
			<div class="col-sm-8">
				<select id="qteasy"  name="qteasy" th:value="${question.qteasy}" class="form-control noselect2 selectpicker">
                	<option value=""> -- 请选择难易度 -- </option>
                	<option th:selected="${question.qteasy==1}" value="1">非常容易</option>
                	<option th:selected="${question.qteasy==2}" value="2">容易</option>
                	<option th:selected="${question.qteasy==3}" value="3">中等</option>
                	<option th:selected="${question.qteasy==4}" value="4">难度</option>
                	<option th:selected="${question.qteasy==5}" value="5">非常难</option>
                </select>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">是否开放off关闭on开放：</label>
			<div class="col-sm-8">
				<div class="onoffswitch">
					<input type="checkbox" th:checked="${question.status==1?true:false}" class="onoffswitch-checkbox" id="status" name="status">
					<label class="onoffswitch-label" for="status">
						<span class="onoffswitch-switch"></span>
						<span class="onoffswitch-inner"></span>
					</label>
				</div>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">题干：</label>
			<div class="col-sm-8">
				<input id="name" name="name" type="hidden" th:value="${question.name}">
				<div class="ibox-content no-padding">
                    <div class="name" th:utext="${question.name}"></div>
                </div>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">参考答案：</label>
			<div class="col-sm-8">
				<input id="qans" name="qans" class="form-control" type="text" th:value="${question.qans}">
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">选项A：</label>
			<div class="col-sm-8">
				<input id="ansA" name="ansA"  type="hidden" th:value="${question.ansA}">
				<div class="ibox-content no-padding">
                    <div class="ansA" th:utext="${question.ansA}"></div>
                </div>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">选项B：</label>
			<div class="col-sm-8">
				<input id="ansB" name="ansB" type="hidden" th:value="${question.ansB}">
				<div class="ibox-content no-padding">
                    <div class="ansB" th:utext="${question.ansB}"></div>
                </div>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">选项C：</label>
			<div class="col-sm-8">
				<input id="ansC" name="ansC" type="hidden" th:value="${question.ansC}">
				<div class="ibox-content no-padding">
                    <div class="ansC" th:utext="${question.ansC}"></div>
                </div>
			</div>
		</div>
	
			<div class="form-group">
			<label class="col-sm-3 control-label">选项D：</label>
			<div class="col-sm-8">
				<input id="ansD" name="ansD" type="hidden" th:value="${question.ansD}">
				<div class="ibox-content no-padding">
                    <div class="ansD" th:utext="${question.ansD}"></div>
                </div>
			</div>
		</div>
	
	
			<div class="form-group">
			<label class="col-sm-3 control-label">解析：</label>
			<div class="col-sm-8">
				<input id="qnote" name="qnote" type="hidden" th:value="${question.qnote}">
				<div class="ibox-content no-padding">
                    <div class="qnote" th:utext="${question.qnote}"></div>
                </div>
			</div>
		</div>
	
	
	</form>
</div>
<div th:include="include::footer"></div>
    <th:block th:include="include :: summernote-js" />
<script type="text/javascript">
	var prefix = ctx + "exam/question"
	$("#form-question-add").validate({
		onkeyup: false,
		ignore: "",//不验证的元素
		rules:{
			kbid:{
				required:true				
			},
			qtid:{
				required:true				
			},
			qteasy:{
				required:true,
				digits:true
			},
			qtid:{
				required:true				
			},
			name:{
				required:true,
				minlength:5
			},
			qans:{
				required:true				
			},
			ansA:{
				required:true,
				minlength:1
			},
			ansB:{
				required:true,
				minlength:1
			}
		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent());
		}
	});

	function submitHandler() {
		var code=$('.name').summernote('code');
		if(textofsumm(code).length==0)$("#name").val("");
		else $("#name").val(Base64.encode(code));
		
		code=$('.ansA').summernote('code');
		if(textofsumm(code).length==0)$("#ansA").val("");
		else $("#ansA").val(Base64.encode(code));
		
		code=$('.ansB').summernote('code');
		if(textofsumm(code).length==0)$("#ansB").val("");
		else $("#ansB").val(Base64.encode(code));
		
		code=$('.ansC').summernote('code');
		if(textofsumm(code).length==0)$("#ansC").val("");
		else $("#ansC").val(Base64.encode(code));
		
		code=$('.ansD').summernote('code');
		if(textofsumm(code).length==0)$("#ansD").val("");
		else $("#ansD").val(Base64.encode(code));
		
		code=$('.qnote').summernote('code');
		if(textofsumm(code).length==0)$("#qnote").val("");
		else $("#qnote").val(Base64.encode(code));
		
		$("#qans").val($("#qans").val().toUpperCase());
		
		if ($.validate.form()) {
			$.operate.save(prefix + "/editDo", $('#form-question-add').serialize());
		}
	}
    function selectTree() {
        var treeId = $("#treeId").val();
    	var id = $.common.isEmpty(treeId) ? "1" : $("#treeId").val();
        var options = {
            title: '选择上级',
            width: "380",
            url: ctx + "exam/qdb/selectTree/" + id,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#qdbid").val(body.find('#treeId').attr("pid"));
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
    }
$(function(){
	createsn();
})
function textofsumm(code){
	return $("<div>").html(code).text();
}
function createsn() {
            $('.name').summernote({ 
            	dialogsInBody: true,
                height : '220px',lang : 'zh-CN', placeholder:"可以图文混排题目信息",
                callbacks: {
                    onImageUpload: function(files) {
                    	uploadpic(files[0],$('.name'));                        
                    }
                }
            });
            //$('.name').summernote('code',$("#name").val());
            $('.ansA').summernote({  
            	dialogsInBody: true,
                height : '120px',lang : 'zh-CN', placeholder:"可以图文混排题目信息",
                callbacks: {
                    onImageUpload: function(files) {
                    	uploadpic(files[0],$('.ansA'));                        
                    }
                }
            });
            $('.ansB').summernote({ 
            	dialogsInBody: true,
                height : '120px',lang : 'zh-CN', placeholder:"可以图文混排题目信息",
                callbacks: {
                    onImageUpload: function(files) {
                    	uploadpic(files[0],$('.ansB'));                        
                    }
                }
            });
            $('.ansC').summernote({  
            	dialogsInBody: true,
                height : '120px',lang : 'zh-CN', placeholder:"可以图文混排题目信息",
                callbacks: {
                    onImageUpload: function(files) {
                    	uploadpic(files[0],$('.ansC'));                        
                    }
                }
            });
            $('.ansD').summernote({    
            	dialogsInBody: true,
                height : '120px',lang : 'zh-CN', placeholder:"可以图文混排题目信息",
                callbacks: {
                    onImageUpload: function(files) {
                    	uploadpic(files[0],$('.ansD'));                        
                    }
                }
            });
            $('.qnote').summernote({    
            	dialogsInBody: true,
                height : '220px',lang : 'zh-CN', placeholder:"可以图文混排题目解析信息",
                callbacks: {
                    onImageUpload: function(files) {
                    	uploadpic(files[0],$('.qnote'));                        
                    }
                }
            });
        };
        function uploadpic(file,o){
        	var formData = new FormData();
            formData.append("file", file);
            formData.append("filetype", "img");
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: formData,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                    	var imgNode = document.createElement("img");
			            imgNode.src = ctx+"common/vimg?n="+result.url;
			            o.summernote('insertNode', imgNode);
                        $(".note-image-input").removeAttr("name");
                    } else {
                        $.modal.alertError(result.msg);
                        $(".note-image-input").removeAttr("name");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }

    </script>
</body>
</html>
